//package com.ding.config;
//
//import com.ding.entity.RuleInfo;
//import com.ding.service.RuleService;
//import org.kie.api.KieBase;
//import org.kie.api.KieServices;
//import org.kie.api.builder.KieRepository;
//import org.kie.api.io.Resource;
//import org.kie.api.io.ResourceType;
//import org.kie.api.runtime.KieContainer;
//import org.kie.api.runtime.KieSession;
//import org.kie.internal.builder.KnowledgeBuilder;
//import org.kie.internal.builder.KnowledgeBuilderFactory;
//import org.kie.internal.io.ResourceFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//
///**
// * @author ding
// * @date 2021/6/4 16:55
// */
//
//@Configuration
//public class DroolsConfig {
//
//    private final KieServices kieServices = KieServices.Factory.get();
//
//    @Autowired
//    private RuleService ruleService;
//
//    @Bean
//    public KieBase knowledgeBuilder() {
//        KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
//        generatorRule(knowledgeBuilder);
//        KieBase kieBase = knowledgeBuilder.newKieBase();
//        return kieBase;
//    }
//
//    private void generatorRule(KnowledgeBuilder knowledgeBuilder) {
//        List<RuleInfo> allRules = ruleService.getAllRules();
//        for (RuleInfo rule : allRules) {
//            Resource resource = ResourceFactory.newByteArrayResource(rule.getContent().getBytes(StandardCharsets.UTF_8));
//            knowledgeBuilder.add(resource, ResourceType.DRL);
//        }
//    }
//    @Bean
//    @ConditionalOnMissingBean
//    public KieContainer kieContainer() throws IOException {
//        KieRepository kieRepository = kieServices.getRepository();
////        KieResources resources = kieServices.getResources();
////        Resource resource = resources.newInputStreamResource();
//
//        kieRepository.addKieModule(kieRepository.getKieModule(kieRepository.getDefaultReleaseId()));
//        return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
//    }
//
//    @Bean
//    public KieSession kieSession(KieBase kieBase){
//        return kieBase.newKieSession();
//    }
//
//}
